package com.ylkj.wx.controller;



import java.io.File;
import java.io.FileInputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ylkj.common.base.ResultData;
import com.ylkj.common.base.ReturnParamData;
import com.ylkj.wx.entitis.WXPicInfo;
import com.ylkj.wx.facade.WXInfoFacade;
import com.ylkj.wx.utils.QiniuUtil;
import com.ylkj.wx.utils.WeixCommonUtil;
import com.ylkj.ylboss.rest.service.UploadImRecordService;
import com.ylkj.ylboss.rest.vo.ImRecordVO;

@Controller
@RequestMapping("/media")
public class MediaContrllor {

	@Resource
	private WXInfoFacade wXInfoFacade;
	
	@Autowired
	private UploadImRecordService uploadImRecordService;

	private static final Logger LOGGER = LoggerFactory.getLogger(MediaContrllor.class);

	@RequestMapping(value = "/saveVoice")
	public String saveVoice(HttpServletRequest request) throws Exception {
		ImRecordVO imRecordVO = new ImRecordVO();
		String mediaId = request.getParameter("serverId");
		String queId = request.getParameter("queId");
		String senderPhrCode = request.getParameter("senderPhrCode");
		String sender = request.getParameter("sender");
		String contentType = request.getParameter("contentType");
		String content = request.getParameter("content");
		String answerTime = request.getParameter("answerTime");
		String receiverPhrCode = request.getParameter("receiverPhrCode");
		String receiver = request.getParameter("receiver");
		String busiTypeCode = request.getParameter("busiTypeCode");
		String busiTypeName = request.getParameter("busiTypeName");
		
		imRecordVO.setQueId(queId);
		imRecordVO.setSenderPhrCode(senderPhrCode);
		imRecordVO.setSender(sender);
		imRecordVO.setContentType(contentType);
		imRecordVO.setContent(content);
		imRecordVO.setAnswerLong(answerTime);
		imRecordVO.setReceiverPhrCode(receiverPhrCode);
		imRecordVO.setReceiver(receiver);
		imRecordVO.setBusiTypeCode(busiTypeCode);
		imRecordVO.setBusiTypeName(busiTypeName);
		
		String accessToken = wXInfoFacade.getAccessToken();
		WXPicInfo wxPicInfo = WeixCommonUtil.downloadMedia(accessToken, mediaId);

		LOGGER.info("==后缀吗？====" + wxPicInfo.getFileExt());
		String fileName = System.currentTimeMillis()+"."+wxPicInfo.getFileExt();
		///
        File file=new File("D:\\timg.jpg");//要转换的文件  
        FileInputStream inputStream = new FileInputStream(file); 
        byte[] buffer = new byte[inputStream.available()];
		///
		//new QiniuUtil().upload(wxPicInfo.getMediaData(),fileName );
        new QiniuUtil().upload(buffer,fileName );
		String fileUrl = new QiniuUtil().downloadFile(fileName);
	/*	String root = request.getRealPath(request.getContextPath());
		String fileName = "123."+wxPicInfo.getFileExt();
		OutputStream out = new FileOutputStream(new File(root, fileName));
		out.write(wxPicInfo.getMediaData());
		out.close();*/
		LOGGER.info("==音频名称====" + fileName);
		
		ResultData rd = null;
		try {
			rd = uploadImRecordService.saveImRecord(imRecordVO);
		} catch (Exception e) {
			rd = new ResultData();
			rd.setCode(ReturnParamData.ReturnParam.SYSTEMEXCEPTION.getCode());
			rd.setMessage(ReturnParamData.ReturnParam.SYSTEMEXCEPTION.getMes());
		}
		
		return fileName;
	}

}
